/*
 * @author: Victor
 * @Date: 2021-11-26 15:09:13
 * @LastEditTime: 2021-11-28 12:01:54
 */
$.ajaxPrefilter(function (opt) {
  //  统一拼接根路径
  opt.url = 'http://api-breakingnews-web.itheima.net' + opt.url;
  //  为需要权限接口统一添加 Authorization 头
  if (opt.url.indexOf('/my/') != -1) {
    opt.headers = {
      Authorization: localStorage.getItem('token'),
    };
    opt.complete = function (res) {
      // console.log(res);
      if (
        res.responseJSON.status === 1 &&
        res.responseJSON.message === '身份认证失败！'
      ) {
        // 当前用户为非法用户
        // 清空token
        localStorage.removeItem('token');
        // 强制跳转到login
        location.href = 'login.html';
      }
    };
  }
});
